home *** CD-ROM | disk | FTP | other *** search
- class Intersection
- {
- function Intersection()
- {
- }
- static function DoIntersect(volume1, volume2)
- {
- if(!volume1.IsColliding() || !volume2.IsColliding())
- {
- return null;
- }
- if(!volume1.MayCollideOnSeparation() && !volume2.MayCollideOnSeparation())
- {
- if(volume1.m_body && volume2.m_body)
- {
- var _loc5_ = volume1.m_body._effectiveVelocity.GetSubtract(volume2.m_body._effectiveVelocity);
- var _loc6_ = volume1.m_body._location.GetSubtract(volume2.m_body._location);
- if(_loc5_.DotProduct(_loc6_) > 0)
- {
- return null;
- }
- }
- }
- if(volume1 instanceof CHitTestCollision)
- {
- var _loc4_ = volume1.m_body.GetWorldSpaceLocation();
- return !volume2.m_body.hitTest(_loc4_._x,_loc4_._y,false) ? null : new CCollisionInfo();
- }
- if(volume2 instanceof CHitTestCollision)
- {
- var _loc3_ = volume2.m_body.GetWorldSpaceLocation();
- return !volume1.m_body.hitTest(_loc3_._x,_loc3_._y,false) ? null : new CCollisionInfo();
- }
- if(volume1 instanceof CCircle && volume2 instanceof CCircle)
- {
- return Intersection.DoIntersectCircleCircle(CCircle(volume1),CCircle(volume2));
- }
- if(volume1 instanceof CCVAxisAlignedBox && volume2 instanceof CCVAxisAlignedBox)
- {
- return Intersection.DoIntersectAABAAB(CCVAxisAlignedBox(volume1),CCVAxisAlignedBox(volume2));
- }
- if(volume1 instanceof CLineSegment && volume2 instanceof CCircle)
- {
- return Intersection.DoIntersectCircleLineSegment(CCircle(volume2),CLineSegment(volume1));
- }
- if(volume1 instanceof CCircle && volume2 instanceof CLineSegment)
- {
- return Intersection.DoIntersectCircleLineSegment(CCircle(volume1),CLineSegment(volume2));
- }
- if(volume1 instanceof CCircle && volume2 instanceof CCVAxisAlignedBox)
- {
- return Intersection.DoIntersectCircleAAB(CCircle(volume1),CCVAxisAlignedBox(volume2));
- }
- if(volume1 instanceof CCVAxisAlignedBox && volume2 instanceof CCircle)
- {
- return Intersection.DoIntersectCircleAAB(CCircle(volume2),CCVAxisAlignedBox(volume1));
- }
- if(volume1 instanceof CCVAxisAlignedBox && volume2 instanceof CLineSegment)
- {
- return Intersection.DoIntersectAABLineSegment(CCVAxisAlignedBox(volume1),CLineSegment(volume2));
- }
- if(volume1 instanceof CLineSegment && volume2 instanceof CCVAxisAlignedBox)
- {
- return Intersection.DoIntersectAABLineSegment(CCVAxisAlignedBox(volume2),CLineSegment(volume1));
- }
- if(volume1 instanceof CLineSegment && volume2 instanceof CLineSegment)
- {
- return Intersection.DoIntersectLineSegmentLineSegment(CLineSegment(volume1),CLineSegment(volume2));
- }
- }
- static function DoIntersectCircleCircle(c1, c2)
- {
- var _loc6_ = c1._center.GetDistance(c2._center);
- var _loc4_ = c1._radius + c2._radius - _loc6_;
- if(_loc4_ > 0)
- {
- var _loc1_ = new CCollisionInfo();
- _loc1_.m_delta = c1._center.GetSubtract(c2._center);
- _loc1_.m_normal = _loc1_.m_delta.GetNormal();
- _loc1_.m_interpenetrationDepth = _loc4_;
- var _loc5_ = c1._radius / (c2._radius + c1._radius);
- _loc1_.m_approximateTouchPoint = c1._center.GetAdd(_loc1_.m_delta.GetMultiplyScalar(_loc5_));
- return _loc1_;
- }
- return null;
- }
- static function DoIntersectAABAAB(aabb1, aabb2)
- {
- var _loc5_ = aabb1._left <= aabb2._right && aabb1._right >= aabb2._left;
- var _loc4_ = aabb1._top <= aabb2._bottom && aabb1._bottom >= aabb2._top;
- if(_loc5_ && _loc4_)
- {
- var _loc1_ = new CCollisionInfo();
- _loc1_.m_delta = aabb1._center.GetSubtract(aabb2._center);
- _loc1_.m_normal = _loc1_.m_delta.GetNormal();
- _loc1_.m_interpenetrationDepth = undefined;
- _loc1_.m_approximateTouchPoint = undefined;
- return _loc1_;
- }
- return null;
- }
- static function DoIntersectCircleAAB(circle, aab)
- {
- var _loc3_ = aab._topLeftCorner;
- var _loc2_ = aab._bottomRightCorner;
- var _loc5_ = new Vector2D(_loc2_._x,_loc3_._y);
- var _loc6_ = new Vector2D(_loc3_._x,_loc2_._y);
- var _loc1_ = circle._center;
- var _loc4_ = circle._radius;
- if(_loc1_._x >= _loc3_._x && _loc1_._x <= _loc2_._x && _loc1_._y >= _loc3_._y && _loc1_._y <= _loc2_._y)
- {
- return new CCollisionInfo();
- }
- var _loc10_ = Intersection.IntersectCircleLineSegment(_loc1_,_loc4_,_loc3_,_loc5_);
- var _loc9_ = Intersection.IntersectCircleLineSegment(_loc1_,_loc4_,_loc5_,_loc2_);
- var _loc8_ = Intersection.IntersectCircleLineSegment(_loc1_,_loc4_,_loc2_,_loc6_);
- var _loc7_ = Intersection.IntersectCircleLineSegment(_loc1_,_loc4_,_loc6_,_loc3_);
- if(_loc10_ || _loc9_ || _loc8_ || _loc7_)
- {
- return new CCollisionInfo();
- }
- return null;
- }
- static function DoIntersectLineSegmentLineSegment(segment1, segment2)
- {
- if(segment1._radius > 0 || segment2._radius > 0)
- {
- if(segment1._radius + segment2._radius >= Distance.LineSegmentLineSegment(segment1,segment2))
- {
- return new CCollisionInfo();
- }
- return null;
- }
- return Intersection.IntersectLineSegmentLineSegment(segment1.m_endpoint1,segment1.m_endpoint2,segment2.m_endpoint1,segment2.m_endpoint2);
- }
- static function DoIntersectCircleLineSegment(circle, segment)
- {
- if(segment._radius > 0)
- {
- if(segment._radius >= Distance.CircleLineSegment(circle,segment))
- {
- return new CCollisionInfo();
- }
- return null;
- }
- return Intersection.IntersectCircleLineSegment(circle._center,circle._radius,segment.m_endpoint1,segment.m_endpoint2);
- }
- static function DoIntersectAABLineSegment(aab, segment)
- {
- var _loc3_ = aab._topLeftCorner;
- var _loc2_ = aab._bottomRightCorner;
- var _loc4_ = new Vector2D(_loc2_._x,_loc3_._y);
- var _loc5_ = new Vector2D(_loc3_._x,_loc2_._y);
- if(segment.m_endpoint1._x >= _loc3_._x && segment.m_endpoint1._x <= _loc2_._x && segment.m_endpoint1._y >= _loc3_._y && segment.m_endpoint1._y <= _loc2_._y)
- {
- return new CCollisionInfo();
- }
- if(segment.m_endpoint2._x >= _loc3_._x && segment.m_endpoint2._x <= _loc2_._x && segment.m_endpoint2._y >= _loc3_._y && segment.m_endpoint2._y <= _loc2_._y)
- {
- return new CCollisionInfo();
- }
- if(segment._radius > 0)
- {
- var _loc9_ = new CLineSegment();
- _loc9_.m_endpoint1 = _loc3_;
- _loc9_.m_endpoint2 = _loc4_;
- var _loc8_ = new CLineSegment();
- _loc8_.m_endpoint1 = _loc4_;
- _loc8_.m_endpoint2 = _loc2_;
- var _loc7_ = new CLineSegment();
- _loc7_.m_endpoint1 = _loc2_;
- _loc7_.m_endpoint2 = _loc5_;
- var _loc6_ = new CLineSegment();
- _loc6_.m_endpoint1 = _loc5_;
- _loc6_.m_endpoint2 = _loc3_;
- var _loc13_ = Intersection.DoIntersectLineSegmentLineSegment(segment,_loc9_);
- var _loc12_ = Intersection.DoIntersectLineSegmentLineSegment(segment,_loc8_);
- var _loc11_ = Intersection.DoIntersectLineSegmentLineSegment(segment,_loc7_);
- var _loc10_ = Intersection.DoIntersectLineSegmentLineSegment(segment,_loc6_);
- }
- else
- {
- _loc13_ = Intersection.IntersectLineSegmentLineSegment(segment.m_endpoint1,segment.m_endpoint2,_loc3_,_loc4_);
- _loc12_ = Intersection.IntersectLineSegmentLineSegment(segment.m_endpoint1,segment.m_endpoint2,_loc4_,_loc2_);
- _loc11_ = Intersection.IntersectLineSegmentLineSegment(segment.m_endpoint1,segment.m_endpoint2,_loc2_,_loc5_);
- _loc10_ = Intersection.IntersectLineSegmentLineSegment(segment.m_endpoint1,segment.m_endpoint2,_loc5_,_loc3_);
- }
- if(_loc13_ || _loc12_ || _loc11_ || _loc10_)
- {
- return new CCollisionInfo();
- }
- return null;
- }
- static function IntersectLineSegmentLineSegment(a1, a2, b1, b2)
- {
- var _loc9_ = (b2._x - b1._x) * (a1._y - b1._y) - (b2._y - b1._y) * (a1._x - b1._x);
- var _loc8_ = (a2._x - a1._x) * (a1._y - b1._y) - (a2._y - a1._y) * (a1._x - b1._x);
- var _loc3_ = (b2._y - b1._y) * (a2._x - a1._x) - (b2._x - b1._x) * (a2._y - a1._y);
- if(_loc3_ != 0)
- {
- var _loc7_ = _loc9_ / _loc3_;
- var _loc6_ = _loc8_ / _loc3_;
- if(0 <= _loc7_ && _loc7_ <= 1 && 0 <= _loc6_ && _loc6_ <= 1)
- {
- return new CCollisionInfo();
- }
- return null;
- }
- return null;
- }
- static function IntersectCircleLineSegment(c, r, a1, a2)
- {
- var _loc8_ = (a2._x - a1._x) * (a2._x - a1._x) + (a2._y - a1._y) * (a2._y - a1._y);
- var _loc3_ = 2 * ((a2._x - a1._x) * (a1._x - c._x) + (a2._y - a1._y) * (a1._y - c._y));
- var _loc10_ = c._x * c._x + c._y * c._y + a1._x * a1._x + a1._y * a1._y - 2 * (c._x * a1._x + c._y * a1._y) - r * r;
- var _loc7_ = _loc3_ * _loc3_ - 4 * _loc8_ * _loc10_;
- if(_loc7_ < 0)
- {
- return null;
- }
- if(_loc7_ == 0)
- {
- return null;
- }
- var _loc9_ = Math.sqrt(_loc7_);
- var _loc6_ = (- _loc3_ + _loc9_) / (2 * _loc8_);
- var _loc5_ = (- _loc3_ - _loc9_) / (2 * _loc8_);
- if((_loc6_ < 0 || _loc6_ > 1) && (_loc5_ < 0 || _loc5_ > 1))
- {
- if(_loc6_ < 0 && _loc5_ < 0 || _loc6_ > 1 && _loc5_ > 1)
- {
- return null;
- }
- return new CCollisionInfo();
- }
- return new CCollisionInfo();
- }
- }
-